Static Expression Bounds
The bound for the @numelts qualifier must be a static expression. A static expression is either a constant expression, or an expression involving valueof(T) for a type-level expression T. The valueof construct is used to connect the value of a run-time integer to the static bound on an array. For example, the following function takes in an integer num and pointer to a sequence of num integers and returns the sum of the sequence:
int sum(tag_t<`n> num,
int *@notnull @numelts(valueof(`n)) p) {
int a = 0;
for (unsigned i = 0; i < num; i++)
a += p[i];
}
The type of num is specified as tag_t<`n>
. This simply means that
num holds an integer value, called `n
, and the number of elements
of p is equal to n. This form of dependency is common enough that it
can be abbreviated as follows:
int sum(tag_t num, int p[num]);
and the compiler will fill in the missing information.